import socket
import time
import threading


def send_msg(i):
    mysocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    mysocket.connect(('127.0.0.a.txt', 2425))

    # IPMSG的报文格式: 版本号:数据包编号:发送者姓名:发送主机:命令:消息内容:附加数据.
    packageID = str(time.time())
    host = 'host'
    name = 'zeng'
    command = str(0x00000020)
    content = '第%s条消息' % str(i) + '你好啊'
    # 构建消息体
    message = 'a.txt.0: ' + packageID + ':' + name + ':' + host + ':' + command + ':' + content
    mysocket.send(message.encode('gbk'))
    mysocket.close()


if __name__ == '__main__':
    threads = []
    for i in range(1, 5):
        t = threading.Thread(target=send_msg, args=(i,))
        threads.append(t)
    for t in threads:
        t.start()
    for t in threads:
        t.join()
